#=============================================================================#
#
# PROJECT:        Who Pays for Peace?
# AUTHORS:        ** anonymized for review **
# CONTACT:        ** anonymized for review **
# LAST MODIFIED:  July 7, 2022
# 
#=============================================================================#
#
# This R file contains the code used to replicate the average treatment effects 
# (at Wave 2)
# 
#=============================================================================#


# Initial settings ------------------------------------------------------------

#rm(list=ls())
#getwd()

## Install and load all necessary packages ------------------------------------
# ipak function: install and load multiple R packages.
# check to see if packages are installed. Install them if they are not, then load them into the R session.

ipak <- function(pkg){  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("effectsize", "stargazer") 
ipak(packages)


## Load the data --------------------------------------------------------------
mydata.t2 <- readRDS("data/T2-clean-data.rds")


# ATEs: OLS regressions -------------------------------------------------------

## PROVISION 1: Route 1 (r1) --------------------------------------------------
mydata.t2$route_1_condition <- relevel(mydata.t2$route_1_condition, 
                                      ref = "Control")#set reference category
lm_r1 <- lm(route_1 ~ route_1_condition, 
            data = mydata.t2)#fit simple OLS regression
summary(lm_r1)#check unstanderdized results
effectsize(lm_r1) #standardized coefficients (factors are not standardized!)


## PROVISION 2: Route 2 (r2) --------------------------------------------------
mydata.t2$route_2_condition <- relevel(mydata.t2$route_2_condition, 
                                       ref = "Control")
lm_r2 <- lm(route_2 ~ route_2_condition, data = mydata.t2)
summary(lm_r2)
effectsize(lm_r2)


## PROVISION 3: Trust workshops (t) -------------------------------------------
mydata.t2$trust_condition <- relevel(mydata.t2$trust_condition, 
                                     ref = "Control")
lm_t <- lm(trust ~ trust_condition, data = mydata.t2)
summary(lm_t)
effectsize(lm_t)


## PROVISION 4: Clear mines (m) -----------------------------------------------
mydata.t2$mines_condition <- relevel(mydata.t2$mines_condition, 
                                     ref = "Control")
lm_m <- lm(mines ~ mines_condition, data = mydata.t2)
summary(lm_m)
effectsize(lm_m)


## PROVISION 5: Aid for military personnel (aid) ------------------------------
mydata.t2$aid_condition <- relevel(mydata.t2$aid_condition, 
                                   ref = "Control")
lm_aid <- lm(aid ~ aid_condition, data = mydata.t2)
summary(lm_aid)
effectsize(lm_aid)


## PROVISION 6: Release prisoners of war (pow) --------------------------------
mydata.t2$pow_condition <- relevel(mydata.t2$pow_condition, 
                                   ref = "Control")
lm_pow <- lm(pow ~ pow_condition, data = mydata.t2)
summary(lm_pow)
effectsize(lm_pow)


## PROVISION 7: Return of displaced people (return) ---------------------------
mydata.t2$return_condition <- relevel(mydata.t2$return_condition, 
                                  ref = "Control")
lm_dp <- lm(return ~ return_condition, data = mydata.t2)
summary(lm_dp)
effectsize(lm_dp)


# Table 2 ---------------
names(lm_r1$coefficients)[1] <- "(intercept)"
names(lm_r2$coefficients)[1] <- "(intercept)"
names(lm_t$coefficients)[1] <- "(intercept)"
names(lm_m$coefficients)[1] <- "(intercept)"
names(lm_aid$coefficients)[1] <- "(intercept)"
names(lm_pow$coefficients)[1] <- "(intercept)"
names(lm_dp$coefficients)[1] <- "(intercept)"

names(lm_r1$coefficients)[2] <- "Armenia"
names(lm_r2$coefficients)[2] <- "Armenia"
names(lm_t$coefficients)[2] <- "Armenia"
names(lm_m$coefficients)[2] <- "Armenia"
names(lm_aid$coefficients)[2] <- "Armenia"
names(lm_pow$coefficients)[2] <- "Armenia"
names(lm_dp$coefficients)[2] <- "Armenia"

names(lm_r1$coefficients)[3] <- "Azerbaijan"
names(lm_r2$coefficients)[3] <- "Azerbaijan"
names(lm_t$coefficients)[3] <- "Azerbaijan"
names(lm_m$coefficients)[3] <- "Azerbaijan"
names(lm_aid$coefficients)[3] <- "Azerbaijan"
names(lm_pow$coefficients)[3] <- "Azerbaijan"
names(lm_dp$coefficients)[3] <- "Azerbaijan"

names(lm_r1$coefficients)[4] <- "Both"
names(lm_r2$coefficients)[4] <- "Both"
names(lm_t$coefficients)[4] <- "Both"
names(lm_m$coefficients)[4] <- "Both"
names(lm_aid$coefficients)[4] <- "Both"
names(lm_pow$coefficients)[4] <- "Both"
names(lm_dp$coefficients)[4] <- "Both"

names(lm_r1$coefficients)[5] <- "IC"
names(lm_r2$coefficients)[5] <- "IC"
names(lm_t$coefficients)[5] <- "IC"
names(lm_m$coefficients)[5] <- "IC"
names(lm_aid$coefficients)[5] <- "IC"


stargazer(lm_r1, lm_r2, lm_t, lm_m, lm_aid, lm_pow, lm_dp,
          title = "Average Treatment Effects for New Peace Provisions at Wave 2.",
          model.numbers = F,
          ci = T,
          omit.stat = c("ser", "f"),
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(.05, .01, .001),
          notes = c("Cell entries are unstandardized OLS coefficients with confidence intervals (CIs) in parentheses. Support for each peace provision is measured on a 7-point scale. Route 1 = New route connecting the Nakhchivan Autonomous Republic and Azerbaijan. Route 2 = New route connecting Nagorno-Karabakh and Armenia. Trust = Trust- and cohesion-building workshops between Armenian and Azerbaijani citizens. Mines = Clearing towns, villages, roads, and the land around them of mines and unexploded ordnance. Aid = Aid to military personnel left disabled by the war and to the families of those killed. POW = Release of all prisoners of war, hostages, and other detained persons. Displaced = Return of people displaced during the 1994 and 2020 conflict. Full question wording in Appendix A.2. * p<0.05; ** p<0.01; *** p<0.001 (two-tailed)"),
          align = TRUE, notes.align = "l", notes.append = F,
          out = "tables/table2.html")

